<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>表格Demo</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../assets/libs/layui/css/layui.css" media="all">
</head>

<body>

    <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
        <legend>修改菜谱</legend>
    </fieldset>


    <form class="layui-form" action="" lay-filter="example" method="POST" action="" style="padding: 10px 50px;">

        <div class="layui-form-item" style="margin-top: 20px">
            <label class="layui-form-label">菜谱编号:</label>
            <div class="layui-input-block" style="margin-right: 20px">
                <input type="text" name="menuId" disabled="true" autocomplete="off" id="menuId" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">食谱名称</label>
            <div class="layui-input-block">
                <input type="text" name="menuName" id="menuName" lay-verify="required" placeholder="请输入" required
                    autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">食谱标签</label>
            <div class="layui-input-block" id="menutag">
                <div class="layui-btn-container tag" lay-filter="demotag" lay-newTag="true" style="padding-top: 5px;"
                    id="demotag">
                </div>
            </div>
        </div>

        <div class="layui-form-item" style="margin-top: 20px">
            <label class="layui-form-label">食谱类型</label>
            <div class="layui-input-block">
                <select name="menuFlavor" id='menuFlavor'>
                    <option value="清淡">清淡</option>
                    <option value="微辣">微辣</option>
                    <option value="中辣">中辣</option>
                    <option value="麻辣">麻辣</option>
                </select>
            </div>
        </div>

        <div class="layui-form-item" style="margin-top: 20px">
            <label class="layui-form-label">食谱类型</label>
            <div class="layui-input-block">
                <select name="menuType" id='menuType'>

                </select>
            </div>
        </div>


        <div class="layui-form-item">
            <label class="layui-form-label">食材价格</label>
            <div class="layui-input-block">
                <input type="text" name="menuPrice" id="menuPrice" lay-verify="required" placeholder="请输入" type="number"
                    autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">菜谱热量</label>
            <div class="layui-input-block">
                <input type="text" name="menuHot" id="menuHot" lay-verify="required" placeholder="请输入" type="number"
                    autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item" style="margin-top: 20px">
            <label class="layui-form-label">食谱时区</label>
            <div class="layui-input-block">
                <select name="menuTime" id='menuTime'>
                    <option value="早餐">早餐</option>
                    <option value="午餐">午餐</option>
                    <option value="晚餐">晚餐</option>
                </select>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">发布时间</label>
            <div class="layui-input-block">
                <input name="menuDate" id="menuDate" required lay-verify="required" placeholder="请输入" required
                    class="layui-input"></input>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">食谱简介</label>
            <div class="layui-input-block">
                <textarea name="menuComm" id="menuComm" required lay-verify="required" placeholder="请输入"
                    class="layui-textarea"></textarea>
            </div>
        </div>


        <div class="layui-upload">
            <button type="button" class="layui-btn" id="uploadBtn">食谱图片</button>
            <div class="layui-upload-list" id="previewBox">

            </div>
        </div>


        <div class="layui-form-item" style="margin-top: 20px;">
            <div class="layui-form-label">食谱步骤</div>
            <textarea id="menuMaterials" required lay-verify="required" placeholder="请输入"
                name="menuMaterials"></textarea>
        </div>


        <div class="layui-form-item" style="margin-top: 40px;">
            <div class="layui-input-block">
                <!-- lay-submit="" -->
                <button type="button" class="layui-btn" id="submit" lay-filter="demo1">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
    <style>
        .layui-upload-list {
            padding: 10px 40px;
            display: flex;
            min-height: 150px;
            min-width: 150px;
            border: 1px solid #cccccc;
        }

        .imgitem {
            cursor: pointer;
            background-color: #FF5722;
            ;
            width: 100%;
            text-align: center;
        }

        .imgBox {
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: center;
            margin: 0px 20px;
        }
    </style>
    <script src="../../assets/libs/layui/layui.js" charset="utf-8"></script>
    <script src="../js/util.js" charset="utf-8"></script>

    <script>
        layui.config({
            base: '/module/'
        }).extend({
            tag: 'tag/tag'
        }).use(['upload', 'tag', 'config', 'form', 'jquery', 'upload', 'laydate', 'layedit'], function () {
            var tag = layui.tag;
            var config = layui.config;
            var form = layui.form;
            var $ = layui.jquery;
            var upload = layui.upload;
            var laydate = layui.laydate;
            var layedit = layui.layedit;

            var srcArr = new Array();
            var imgUrl = ''
            var files = [];
            var ieditor;

            tag.render("demotag", {
                skin: 'layui-btn layui-btn-primary layui-btn-sm layui-btn-radius',
                tagText: '<i class="layui-icon layui-icon-add-1"></i>添加标签'
            });
            $.ajax({
                url: config.base_server + 'getMenutypeAll',
                dataType: 'json',
                type: 'get',
                async: false,
                success: function (res) {
                    data = res.data;
                    $.each(data, function (index, item) {
                        $('#menuType').append(new Option(item.menutypeTitle));
                    });
                    form.render("select");
                }
            })
            upload.render({
                elem: '#uploadBtn'
                , url: ''
                , multiple: true
                , bindAction: "#save"
                , auto: false
                , choose: (obj) => {//预览图片
                    files = obj.pushFile();
                    obj.preview((index, file, result) => {
                        imgUrl = result
                        $('#previewBox').append('<div class="imgBox" ><img src="' + result + '" alt="' + file.name + '" class="layui-upload-img" width="100" height="100"><a class="remove_' + index + ' imgitem">取消</a></div>')
                        $('.remove_' + index).bind('click', function () {
                            delete files[index];//删除指定图片
                            $(this).parent().remove();
                        })
                    });
                }
            });

            var req = GetRequest();
            var no = req['editNo'];
            layedit.set({

                uploadImage: {
                    url: config.base_server + 'menuMaterialsuploadImg',
                    accept: 'image',
                    acceptMime: 'image/*',
                    exts: 'jpg|png|gif|bmp|jpeg',
                    size: '10240'
                }
                //右键删除图片/视频时的回调参数，post到后台删除服务器文件等操作，
                //传递参数：
                //图片： imgpath --图片路径
                //视频： filepath --视频路径 imgpath --封面路径
                , calldel: {
                    url: '/Attachment/DeleteFile'
                }
                //开发者模式 --默认为false
                , devmode: true
                //插入代码设置
                , codeConfig: {
                    hide: true,  //是否显示编码语言选择框
                    default: 'javascript' //hide为true时的默认语言格式
                }
                , tool: [
                    'html', 'code', 'strong', 'italic', 'underline', 'del', 'addhr', '|', 'fontFomatt', 'colorpicker', 'face'
                    , '|', 'left', 'center', 'right', '|', 'image_alt', '|', 'fullScreen'
                ]
                , height: '400px'
            });


            $.ajax({
                url: config.base_server + 'getMenuById?editNo=' + no,
                dataType: 'json',
                type: 'get',
                success: function (data) {
                    data = data.data;
                    $('#menuId').val(data.menuId);
                    $('#menuName').val(data.menuName);
                    $('#menuPrice').val(data.menuPrice);
                    $('#menuHot').val(data.menuHot);

                    $('#menuComm').val(data.menuComm);

                    $.each(data.menuTag, function (index, item) {
                        $('#demotag').prepend(`<button  type="button" class="tag-item">${item}</button>`);
                    });
                    tag.render('demotag');
                    var select = 'dd[lay-value=' + data.menuType + ']';
                    $('#menuType').siblings("div.layui-form-select").find('dl').find(select).click();

                    var select = 'dd[lay-value=' + data.menuTime + ']';
                    $('#menuTime').siblings("div.layui-form-select").find('dl').find(select).click();

                    var select = 'dd[lay-value=' + data.menuFlavor + ']';
                    $('#menuFlavor').siblings("div.layui-form-select").find('dl').find(select).click();

                    laydate.render({
                        elem: '#menuDate',
                        value: data.menuDate
                        , type: 'datetime'
                        , trigger: 'click'
                    });
                    $('#menuMaterials').html(data.menuMaterials)
                    ieditor = layedit.build('menuMaterials');
                    srcArr = data.menuImgList;
                    srcArr.forEach((item, index) => {
                        $('#previewBox').append('<div class="imgBox" ><img src="' + item + '" alt="" class="layui-upload-img" width="100" height="100"><a class="remove_' + index + ' imgitem">取消</a></div>');
                        $('.remove_' + index).bind('click', function () {
                            let itemIndex = $(this).parent().parent().find('.imgBox').index($(this).parent()[0]);
                            srcArr.splice(itemIndex, 1);
                            $(this).parent().remove();
                        })
                    })
                    form.render();
                }
            })


            $('#submit').on('click', function () {
                layedit.sync(ieditor)
                var data = form.val('example');
                var { menuName, menuType, menuPrice, menuTime, menuDate, menuComm, menuMaterials, menuId, menuFlavor, menuHot } = data
                var menuTag = $.makeArray($('.tag[lay-filter="demotag"]>.tag-item').map(function () {
                    return $(this).text()
                }))
                var formData = new FormData();

                formData.append('menuName', menuName);
                formData.append('menuId', menuId);
                formData.append('menuPrice', menuPrice);
                formData.append('menuHot', menuHot);

                formData.append('menuTag', JSON.stringify(menuTag));
                formData.append('menuDate', menuDate);
                formData.append('menuComm', menuComm);
                formData.append('menuType', menuType);
                formData.append('menuTime', menuTime);
                formData.append('menuFlavor', menuFlavor);
                formData.append('menuMaterials', menuMaterials)
                for (var i = 0; i < Object.values(files).length; i++) {
                    formData.append('menuImg', Object.values(files)[i]);
                }
                formData.append("menuImgList", JSON.stringify(srcArr));
                $.ajax({
                    type: "POST",
                    url: config.base_server + "updataMenu",
                    async: false,
                    data: formData,
                    processData: false,   //  告诉jquery不要处理发送的数据
                    contentType: false,
                    success: function (data) {
                        if (data.code == 1) {
                            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                            alert("修改成功");
                            parent.layer.close(index); //再执行关闭   
                        } else {
                            alert(data.msg);
                        }
                    },
                    error() {
                        alert("文件太大")
                    }
                });
                return false;
            });

        });
    </script>


</body>